package com.rmb.util;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 *@ClassName AddressUtils           
 *@Description TODO               
 *@Author   lvshook                               
 *@Date 2021/1/20 12:02      
 *@Version 1.0                        
**/public class AddressUtils {

    /**
     * 获取登录地址
     * @param strip
     * @return
     */
    public static String getaddressbyip(String strip) {
        try {

            URL url = new URL("http://opendata.baidu.com/api.php?query=" + strip+"+&+co=&resource_id=6006&t=1433920989928&ie=utf8&oe=utf-8&format=json");;
            URLConnection conn = url.openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            String line = null;
            StringBuffer result = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }
            reader.close();
            cn.hutool.json.JSONObject jsStr = new cn.hutool.json.JSONObject(result.toString());
            JSONArray jsData = (JSONArray) jsStr.get("data");
            //位置
            cn.hutool.json.JSONObject data= (JSONObject) jsData.get(0);
            return (String) data.get("location");
        } catch (IOException e) {
            return "读取失败";
        }
    }

}
